import wepy from 'wepy'
import Dialog from '@/assets/vant/dialog/dialog';
export default class extends wepy.mixin {
  data = {
    cartList: [],
    empty: false
  }

  config = {
  }

  methods = {
    changeNum(id, e) {
      this.$parent.changeNum(id, e.detail)
    },
    goodCheck(id, e) {
      this.$parent.goodCheck(id, e.detail)
    },
    // 删除商品
    deleteGood(id, e) {
      const { instance } = e.detail;
      Dialog.confirm({
        message: '确定删除此商品吗？'
      }).then(() => {
        instance.close()
        this.$parent.deleteGood(id)
        this.$apply()
      }).catch(() => {
        instance.close()
      })
    },
    // 全选
    checkAll(e) {
      this.$parent.checkAll(e.detail)
    },
    // 提交订单
    submit() {
      if (this.totalPrice <= 0) {
        return wepy.showToast({
          title: '请选择商品',
          icon: 'none'
        })
      } else {
        wepy.navigateTo({
          url: '/pages/order'
        })
      }
    }
  }
  computed = {
    isEmpty() {
      return this.cartList.length <= 0 ? true : false
    },
    checked() {
      return !this.cartList.some(x => x.isCheck === false)
    },
    totalPrice() {
      let totalPrice = 0
      this.cartList.forEach(x => x.isCheck ? totalPrice += x.price * x.count : totalPrice)
      return totalPrice * 100
    }
  }
  onLoad() {
    this.cartList = this.$parent.globalData.cart
  }
  onShow() {
    this.$parent.changeBandge()
  }
}
